package org.base.baseproject.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.base.baseproject.utils.UserInfoUtils;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义字段填充
 */
@Component
public class EntityAutoFill implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createBy", String.class, UserInfoUtils.getUserId());
        this.strictInsertFill(metaObject, "updateBy", String.class,UserInfoUtils.getUserId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class,LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class,LocalDateTime.now());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateBy", String.class,UserInfoUtils.getUserId());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class,LocalDateTime.now());
    }
}
